<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initOpenId () {
        if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            $openIdNamespace = new Zend_Session_Namespace('open');
            $openId = $openIdNamespace->openId;
            if (empty($openId)) {
                if (!array_key_exists('code', $_GET)) {
                    $url = 'http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$_SERVER['HTTP_HOST'].(($_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT']).$_SERVER['REQUEST_URI'];
                    include_once(APPLICATION_PATH . "/../library/WxPayPubHelper/WxPayPubHelper.php");
                    $jsApi = new JsApi_pub();
                    #$url = $jsApi->createOauthUrlForCode('http://shop.renrencaichang.com/');
                    $url = $jsApi->createOauthUrlForCode($url);
                    Header("Location: $url");
                } else {
                    $code = $_GET['code'];
                    $wxapi = new Application_Model_WXApi();
                    $openId = $wxapi->getOpenId($code);
                    $openIdNamespace->openId = $openId;
                }
            }
        }
    }

}

